// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import http from './utils/request'
import { powerRouter } from './router/access'
import store from './store'
import 'lib-flexible/flexible'
import Vant from 'vant'
import 'vant/lib/vant-css/index.css'

Vue.config.productionTip = false
Vue.prototype.$http = http
Vue.use(Vant)

const whiteList = ['/login', '/register', '/forget', '/unauthority', '/mine']
router.beforeEach((to, from, next) => {
  if (store.getters.user) { // 判断状态 是否存在
    if (store.getters.newrouter.length !== 0) {
      next()
    } else {
      let newrouter
      if (store.getters.user.accountStatus === 1) { // 判断权限
        newrouter = powerRouter
      } else {
        let newchildren = powerRouter[0].children.filter(route => {
          if (route.meta) {
            if (route.meta.status === store.getters.user.accountStatus) {
              return true
            }
            return false
          } else {
            return true
          }
        })
        newrouter = powerRouter
        newrouter[0].children = newchildren
      }
      router.addRoutes(newrouter) // 添加动态路由
      store.dispatch('saveStatus', newrouter).then(res => {
        next({ ...to })
      }).catch(() => {

      })
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/unauthority')
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
